package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Member;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private PermissionDao permissionDao;
    //findByUsername
    @Override
    public User findByUsername(String username) {
        User user = userDao.findByUsername(username);
        if(user == null){
            return null;
        }
        Integer id = user.getId();
        Set<Role> roles =  roleDao.findByUserId(id);
        if(roles != null && roles.size()>0) {
            for (Role role : roles) {
                Integer roleId = role.getId();
                Set<Permission> permissions = permissionDao.findByRoleId(roleId);
                if (permissions != null && permissions.size() > 0) {
                    role.setPermissions(permissions);
                }
            }
            user.setRoles(roles);
        }
        return user;
    }



    public void add(User user, Integer[] roleIds) {
        userDao.add(user);
        if(roleIds != null && roleIds.length>0) {
            setUserAndRole(user.getId(), roleIds);
        }

    }

    //分页查询
    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<Member> page=userDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public User findById(Integer id) {
        return userDao.findById(id);
    }


    //中间表中插入数据
    private void setUserAndRole(Integer userId, Integer[] roleIds) {
        if(roleIds != null && roleIds.length>0){
            for (Integer roleId : roleIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("user_id",userId);
                map.put("role_id",roleId);
                userDao.setUserAndRole(map);
            }
        }
    }

//    //根据id查询
//    public User findById(int id) {
//        return userDao.findById(id);
//
//    }


    //查询setmealId查询检查组
    public List<Integer> findRoleByUserId(Integer id) {
        return userDao.findRoleByUserId(id);

    }


    public void edit(User user, Integer[] roleIds) {
        //根据检查组id删除关联表
        userDao.deleteAssociation(user.getId());
        //向中间表中插入新连接关系
        setUserAndRole(user.getId(),roleIds);
        //更新检查组信息
        userDao.edit(user);
    }


    @Override
    public void delete(Integer id) throws RuntimeException{
        //根据setmealId从中间表中查询，如果count>0，则表示中间表中有关系不能删除，否则可以删除
        long count =  userDao.findCountByUserId(id);
        if(count >0){
            throw new RuntimeException("当前检查组被引用，不能删除");
        }
        userDao.deleteById(id);
    }

    @Override
    public List<User> findAll() {
        return userDao.findAll();
    }


    public List<Map<String, Object>> findUserCount() {

        //查询套餐数据
        return userDao.findUserCount();
    }
}